package com.zhiyou.teacher.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import com.zhiyou.teacher.model.User;
import com.zhiyou.teacher.service.IUserService;
import com.zhiyou.teacher.common.model.PageResult;
import com.zhiyou.teacher.common.model.Result;

/**
 * 
 *
 * @author hqb
 * @date 2020-01-17 16:10:12
 */
@Slf4j
@RestController
@Api(tags = "")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "姓名", required = false, dataType = "String")
    })
    @GetMapping("/user")
    public PageResult list(@RequestParam Map<String, Object> params) {
        return userService.findList(params);
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/user/{id}")
    public Result findUserById(@PathVariable Long id) {
        User model = userService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping("/user")
    public Result save(@RequestBody User user) {
        userService.saveOrUpdate(user);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/user/{id}")
    public Result delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.succeed("删除成功");
    }
}
